﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace KD.Sms.UI
{
    public class FormHelper
    {

        /// <summary>
        /// 委托 清除RichTextBox中的文本
        /// </summary>
        /// <param name="ricMsg"></param>
        private delegate void ClearText(RichTextBox ricMsg);
        /// <summary>
        ///  委托
        /// </summary>
        /// <param name="ricMsg"></param>
        /// <param name="type"></param>
        /// <param name="_msg"></param>
        private delegate void ApplicationInfomation(RichTextBox ricMsg, MsgType type, string _msg);

        /// <summary>
        /// 委托 设置ComboBox的当前显示文本
        /// </summary>
        /// <param name="box"></param>
        /// <param name="text"></param>
        private delegate void SetComText(ComboBox box, string text);

        /// <summary>
        /// 委托 设置ComboBox的当前显示值
        /// </summary>
        /// <param name="box"></param>
        /// <param name="index"></param>
        private delegate void SetComIndex(ComboBox box, int index);

        /// <summary>
        /// 委托 获取ComboBox选中值
        /// </summary>
        /// <param name="box"></param>
        private delegate object GetComSelectedValue(ComboBox box);

        /// <summary>
        /// 委托 获取ComboBox选中item
        /// </summary>
        /// <param name="box"></param>
        /// <param name="items"></param>
        private delegate object GetComSelectedItem(ComboBox box, ref object items);

        /// <summary>
        /// 委托 获取ComboBox所有item
        /// </summary>
        /// <param name="box"></param>
        private delegate object GetComItems(ComboBox box);

        /// <summary>
        /// 委托 设置控件显示文本
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetConText(Control control, string value);

        /// <summary>
        /// 委托 获取控件文本
        /// </summary>
        /// <param name="control"></param>
        private delegate object GetConText(Control control);

        /// <summary>
        /// 委托 设置控件是否显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetConEnabled(Control control, bool value);
        private delegate void SetConVisable(Control control, bool value);

        /// <summary>
        /// 委托 设置控件背景图片
        /// </summary>
        /// <param name="pic"></param>
        /// <param name="img"></param>
        private delegate void SetImage(Control pic, Image img);

        /// <summary>
        /// 委托 设置控件前景色
        /// </summary>
        /// <param name="ctr"></param>
        /// <param name="co"></param>
        private delegate void SetFore(Control ctr, Color co);

        /// <summary>
        /// 委托 设置控件后景色
        /// </summary>
        /// <param name="ctr"></param>
        /// <param name="co"></param>
        private delegate void SetBack(Control ctr, Color co);


        /// <summary>
        /// 委托 设置控件宽度
        /// </summary>
        /// <param name="ctr"></param>
        /// <param name="width"></param>
        private delegate void SetWidth(Control ctr, int width);

        /// <summary>
        /// 委托 设置CheckBox的值
        /// </summary>
        /// <param name="box"></param>
        /// <param name="value"></param>
        private delegate void SetCheckBox(CheckBox box, bool value);

        /// <summary>
        /// 委托 获取CheckBox的值
        /// </summary>
        /// <param name="box"></param>
        private delegate object GetCheckBox(CheckBox box);

        /// <summary>
        /// 委托 设置RadioButton的值
        /// </summary>
        /// <param name="box"></param>
        /// <param name="value"></param>
        private delegate void SetRadioButton(RadioButton box, bool value);

        /// <summary>
        /// 委托 获取RadioButton的值
        /// </summary>
        /// <param name="box"></param>
        private delegate object GetRadioButton(RadioButton box);

        /// <summary>
        /// 委托 设置Panel的高
        /// </summary>
        /// <param name="pan"></param>
        /// <param name="height"></param>
        private delegate void SetPanelHeight(Panel pan, int height);

        /// <summary>
        /// 委托 设置Panel的宽
        /// </summary>
        /// <param name="pan"></param>
        /// <param name="width"></param>
        private delegate void SetPanelWidth(Panel pan, int width);


        /// <summary>
        /// 清除控件
        /// </summary>
        /// <param name="c"></param>
        private delegate void ControlClear(Control c);

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="main"></param>
        /// <param name="child"></param>
        private delegate void ControlAdd(Control main, Control child);

        private delegate void BindGrid(DataGridView grid, System.Data.DataTable source);

        /// <summary>
        /// 显示三状态控件
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        private delegate void ShowThree(KD.Sms.UI.KdRunStopError ts, KD.Sms.UI.ThreeStateValue value);
        /// <summary>
        /// 显示两状态控件
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        private delegate void ShowTwoMake(KD.Sms.UI.KdRunStop ts, KD.Sms.UI.TwoStateValue value);

        /// <summary>
        /// 显示两状态控件
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        private delegate void ShowTwo(KD.Sms.UI.KdOpenColse ts, KD.Sms.UI.TwoStateValue value);

        /// <summary>
        /// 显示两状态控件
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        private delegate void ShowTwoRunStop(KD.Sms.UI.KdRunStop ts, KD.Sms.UI.TwoStateValue value);

        /// <summary>
        /// 清理RichTextBox
        /// </summary>
        public static void ApplicationClear(RichTextBox ricMsg)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (ricMsg.InvokeRequired)
                {
                    ClearText myInvoke = ApplicationClear;
                    ricMsg.Invoke(myInvoke, ricMsg);
                }
                else
                {
                    ricMsg.Clear();
                }
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void DoBindDataGrid(DataGridView grid, System.Data.DataTable source)
        {
            if (grid != null && !grid.IsDisposed)
            {
                //在线程里以安全方式调用控件
                if (grid.InvokeRequired)
                {
                    BindGrid _MyInvoke = new BindGrid(DoBindDataGrid);
                    grid.Invoke(_MyInvoke, new object[] { grid, source });
                }
                else
                {
                    grid.DataSource = source;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void RicMsgShow(RichTextBox ricMsg, MsgType _type, string _message)
        {
            //在线程里以安全方式调用控件
            if (ricMsg.InvokeRequired)
            {
                ApplicationInfomation _MyInvoke = new ApplicationInfomation(RicMsgShow);
                ricMsg.Invoke(_MyInvoke, new object[] { ricMsg, _type, _message });
            }
            else
            {
                Color co;
                switch (_type)
                {
                    case MsgType.Error:
                        co = Color.Red;
                        break;
                    case MsgType.Waring:
                        co = Color.Blue;
                        break;
                    default:
                        co = Color.Green;
                        break;
                }
                int len = ricMsg.TextLength;
                ricMsg.Select(len, 0);
                ricMsg.SelectionColor = co;
                ricMsg.ScrollToCaret();//让滚动条拉到最底处               
                ricMsg.AppendText( DateTime.Now.ToString("HH:mm:ss") + "  " + _message+ "\r\n");
                KD.Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Program, Log.LogSubitem.Log, _message);
            }
        }

        /// <summary>
        /// 设置ComboBox显示文本
        /// </summary>
        /// <param name="box"></param>
        /// <param name="text"></param>
        public static void SetComboxText(ComboBox box, string text)
        {
            //在线程里以安全方式调用控件
            if (box.InvokeRequired)
            {
                SetComText myInvoke = SetComboxText;
                box.Invoke(myInvoke, box, text);
            }
            else
                box.Text = text;
        }

        /// <summary>
        /// 设置ComboBox显示文本
        /// </summary>
        /// <param name="box"></param>
        /// <param name="text"></param>
        public static void SetComboxTextForFind(ComboBox box, string text)
        {
            //在线程里以安全方式调用控件
            if (box.InvokeRequired)
            {
                SetComText myInvoke = SetComboxText;
                box.Invoke(myInvoke, box, text);
            }
            else
                box.SelectedIndex = box.FindString(text);
        }

        /// <summary>
        /// 设置ComboBox的当前显示值
        /// </summary>
        /// <param name="box"></param>
        /// <param name="index"></param>
        public static void SetComboxIndex(ComboBox box, int index)
        {
            //在线程里以安全方式调用控件
            if (!box.IsDisposed)
            {
                if (box.InvokeRequired)
                {
                    SetComIndex myInvoke = SetComboxIndex;
                    box.Invoke(myInvoke, box, index);
                }
                else
                    box.SelectedIndex = index;
            }
        }

        /// <summary>
        /// 获取ComboBox的SelectedValue
        /// </summary>
        public static object GetComBoxSelectedValue(ComboBox combox)
        {
            //在线程里以安全方式调用控件

            if (combox.InvokeRequired)
            {
                GetComSelectedValue myInvoke = GetComBoxSelectedValue;
                return combox.Invoke(myInvoke, combox);
            }
            return combox.SelectedValue;
        }


        /// <summary>
        /// 获取ComboBox的SelectedItem
        /// </summary>
        public static object GetComBoxSelectedItem(ComboBox combox, ref object item)
        {
            //在线程里以安全方式调用控件
            if (combox.InvokeRequired)
            {
                GetComSelectedItem myInvoke = GetComBoxSelectedItem;
                item = combox.Invoke(myInvoke, combox, "items");
                return combox.Invoke(myInvoke, combox, item);
            }
            else
            {
                return item.ToString() == "items" ? combox.Items : combox.SelectedItem;
            }
        }

        /// <summary>
        /// 获取ComboBox的所有item
        /// </summary>
        public static object GetComBoxItems(ComboBox combox)
        {
            //在线程里以安全方式调用控件

            if (combox.InvokeRequired)
            {
                GetComItems myInvoke = GetComBoxItems;
                return combox.Invoke(myInvoke, combox);
            }

            return combox.Items;

        }

        /// <summary>
        /// 设置控件的Text文本
        /// </summary>
        public static void SetControlText(Control control, string value)
        {
            try
            {

                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetConText myInvoke = SetControlText;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, value);
                        }
                    }
                    else
                        control.Text = value;
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 获取控件的text
        /// </summary>
        public static object GetControlText(Control control)
        {
            //在线程里以安全方式调用控件

            if (control.InvokeRequired)
            {
                GetConText myInvoke = GetControlText;
                return control.Invoke(myInvoke, control);
            }
            return control.Text;

        }

        /// <summary>
        /// 设置控件是否显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        public static void SetControlEnabled(Control control, bool value)
        {
            //在线程里以安全方式调用控件
            if (!control.IsDisposed)
            {
                if (control.InvokeRequired)
                {
                    SetConEnabled myInvoke = SetControlEnabled;
                    control.Invoke(myInvoke, control, value);
                }
                else
                    control.Enabled = value;
            }
        }

        /// <summary>
        /// 设置控件是否显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        public static void SetControlVisable(Control control, bool value)
        {
            //在线程里以安全方式调用控件
            if (!control.IsDisposed)
            {
                if (control.InvokeRequired)
                {
                    SetConVisable myInvoke = SetControlVisable;
                    control.Invoke(myInvoke, control, value);
                }
                else
                    control.Visible = value;
            }
        }

        /// <summary>
        /// 设置控件的背景图片
        /// </summary>
        public static void SetGroundImage(Control pic, Image img)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (pic.InvokeRequired)
                {
                    SetImage myInvoke = SetGroundImage;
                    pic.Invoke(myInvoke, pic, img);
                }
                else
                    pic.BackgroundImage = img;
            }
            catch { }
        }

        /// <summary>
        /// 设置控件的ForeColor
        /// </summary>
        public static void SetControlForeColor(Control control, Color value)
        {
            //在线程里以安全方式调用控件
            if (control.InvokeRequired)
            {
                SetFore myInvoke = SetControlForeColor;
                control.Invoke(myInvoke, control, value);
            }
            else
                control.ForeColor = value;
        }

        /// <summary>
        /// 设置控件的BackGroundColor
        /// </summary>
        public static void SetControlBackColor(Control control, Color value)
        {
            //在线程里以安全方式调用控件
            if (control.InvokeRequired)
            {
                SetFore myInvoke = SetControlBackColor;
                control.Invoke(myInvoke, control, value);
            }
            else
                control.BackColor = value;
        }

        /// <summary>
        /// 设置控件的BackColor
        /// </summary>
        public static void SetBackColor(Control control, Color value)
        {
            //在线程里以安全方式调用控件
            if (control.InvokeRequired)
            {
                SetBack myInvoke = SetBackColor;
                control.Invoke(myInvoke, control, value);
            }
            else
                control.BackColor = value;
        }

        /// <summary>
        /// 设置CheckBox是否选中
        /// </summary>
        public static void SetCheckBoxState(CheckBox box, bool value)
        {
            //在线程里以安全方式调用控件
            if (!box.IsDisposed)
            {
                if (box.InvokeRequired)
                {
                    SetCheckBox myInvoke = SetCheckBoxState;
                    box.Invoke(myInvoke, box, value);
                }
                else
                    box.Checked = value;
            }
        }


        /// <summary>
        /// 设置控件宽度
        /// </summary>
        public static void SetControlWidth(Control ctr, int width)
        {
            //在线程里以安全方式调用控件
            if (!ctr.IsDisposed)
            {
                if (ctr.InvokeRequired)
                {
                    SetWidth myInvoke = SetControlWidth;
                    ctr.Invoke(myInvoke, ctr, width);
                }
                else
                    ctr.Width = width;
            }
        }



        /// <summary>
        /// 获取CheckBox是否选中
        /// </summary>
        public static object GetCheckBoxState(CheckBox box)
        {
            //在线程里以安全方式调用控件
            if (box.InvokeRequired)
            {
                GetCheckBox myInvoke = GetCheckBoxState;
                return box.Invoke(myInvoke, box);
            }
            return box.Checked;
        }

        /// <summary>
        /// 设置RadioButton是否选中
        /// </summary>
        public static void SetRadioButtonState(RadioButton rbButton, bool value)
        {
            //在线程里以安全方式调用控件
            if (!rbButton.IsDisposed)
            {
                if (rbButton.InvokeRequired)
                {
                    SetRadioButton myInvoke = SetRadioButtonState;
                    rbButton.Invoke(myInvoke, rbButton, value);
                }
                else
                    rbButton.Checked = value;
            }
        }

        /// <summary>
        /// 获取RadioButton是否选中
        /// </summary>
        public static object GetRadioButtonState(RadioButton rbButton)
        {
            //在线程里以安全方式调用控件
            if (rbButton.InvokeRequired)
            {
                GetRadioButton myInvoke = GetRadioButtonState;
                return rbButton.Invoke(myInvoke, rbButton);
            }
            return rbButton.Checked;
        }

        /// <summary>
        /// 改变panel的高
        /// </summary>
        /// <param name="pan"></param>
        /// <param name="height"></param>
        public static void ShowPanHeight(Panel pan, int height)
        {
            //在线程里以安全方式调用控件
            if (!pan.IsDisposed)
            {
                //在线程里以安全方式调用控件
                if (pan.InvokeRequired)
                {
                    SetPanelHeight myInvoke = ShowPanHeight;
                    pan.Invoke(myInvoke, pan, height);
                }
                else
                {
                    if (pan.Height != height)
                        pan.Height = height;
                }
            }
        }

        /// <summary>
        /// 改变panel的宽
        /// </summary>
        /// <param name="pan"></param>
        /// <param name="width"></param>
        public static void ShowPanWidth(Panel pan, int width)
        {
            //在线程里以安全方式调用控件
            if (!pan.IsDisposed)
            {
                //在线程里以安全方式调用控件
                if (pan.InvokeRequired)
                {
                    SetPanelWidth myInvoke = ShowPanWidth;
                    pan.Invoke(myInvoke, pan, width);
                }
                else
                {
                    if (pan.Width != width)
                        pan.Width = width;
                }
            }
        }


        /// <summary>
        /// 清理控件
        /// </summary>
        /// <param name="control"></param>
        public static void DoControlClear(Control control)
        {

            //在线程里以安全方式调用控件

            if (control.InvokeRequired)
            {
                ControlClear myInvoke = DoControlClear;
                control.Invoke(myInvoke, control);
            }
            else
                control.Controls.Clear();


        }

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="main"></param>
        /// <param name="child"></param>
        public static void DoControlAdd(Control main, Control child)
        {
            //在线程里以安全方式调用控件
            if (main != null && !main.IsDisposed)
            {
                if (main.InvokeRequired)
                {
                    ControlAdd myInvoke = DoControlAdd;
                    main.Invoke(myInvoke, main, child);
                }
                else
                    main.Controls.Add(child);
            }
        }

        /// <summary>
        /// 显示三态控件值
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        public static void ShowThreeState(KD.Sms.UI.KdRunStopError ts, KD.Sms.UI.ThreeStateValue value)
        {
            if (ts != null && !ts.IsDisposed)
            {
                if (ts.InvokeRequired)
                {
                    ShowThree myInvoke = ShowThreeState;
                    ts.Invoke(myInvoke, ts, value);
                }
                else
                {
                    if (ts.C_Value != value)
                        ts.C_Value = value;
                }
            }
        }

        /// <summary>
        /// 显示两态控件值
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        public static void ShowTwoState(KD.Sms.UI.KdOpenColse ts, KD.Sms.UI.TwoStateValue value)
        {
            try
            {
                if (ts != null && !ts.IsDisposed)
                {
                    if (ts.InvokeRequired)
                    {
                        ShowTwo myInvoke = ShowTwoState;
                        ts.Invoke(myInvoke, ts, value);
                    }
                    else
                    {
                        if (ts.C_Value != value)
                            ts.C_Value = value;
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 显示两态控件值
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        public static void ShowTwoState(KD.Sms.UI.KdRunStop rs, KD.Sms.UI.TwoStateValue value)
        {
            try
            {
                if (rs != null && !rs.IsDisposed)
                {
                    if (rs.InvokeRequired)
                    {
                        ShowTwoRunStop myInvoke = ShowTwoState;
                        rs.Invoke(myInvoke, rs, value);
                    }
                    else
                    {
                        if (rs.C_Value != value)
                            rs.C_Value = value;
                    }
                }
            }
            catch { }
        }




        /// <summary>
        /// 显示两态控件值
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="value"></param>
        public static void ShowTwoStateMake(KD.Sms.UI.KdRunStop ts, KD.Sms.UI.TwoStateValue value)
        {
            if (ts != null && !ts.IsDisposed)
            {
                if (ts.InvokeRequired)
                {
                    ShowTwoMake myInvoke = ShowTwoStateMake;
                    ts.Invoke(myInvoke, ts, value);
                }
                else
                {
                    if (ts.C_Value != value)
                        ts.C_Value = value;
                }
            }
        }


    }

}
